package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.UserFreeze;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.db.UserThaw;
import com.itheima.mapper.UserFreezeMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.mapper.UserThawMapper;
import com.itheima.service.db.UserFreezeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserFreezeServiceImpl implements UserFreezeService {
    @Autowired
    private UserFreezeMapper userFreezeMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserThawMapper userThawMapper;

    @Override
    public UserFreeze findById(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_Id", userId);
        UserFreeze userFreeze = userFreezeMapper.selectOne(queryWrapper);
        return userFreeze;
    }

    @Override
    public Boolean freezeOperation(UserFreeze userFreeze, UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userFreeze.getUserId());

        UserFreeze freeze = userFreezeMapper.selectOne(queryWrapper);
        if (freeze != null) {
            return false;
        }
        userInfoMapper.updateById(userInfo);
        userFreezeMapper.insert(userFreeze);
        return true;
    }

    @Override
    public void freezeUnseal(UserInfo userInfo, UserThaw userThaw) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_Id", userThaw.getUserId());
        userThawMapper.insert(userThaw);
        userFreezeMapper.delete(queryWrapper);
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public List<UserFreeze> findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        return userFreezeMapper.selectList(null);
    }

}
